package cn.psvmc.cxzapi.controller.customer;

import cn.psvmc.cxzapi.model.common.DetailEntity;
import cn.psvmc.cxzapi.model.common.PageEntity;
import cn.psvmc.cxzapi.model.customer.TCustomerBuyout;
import cn.psvmc.cxzapi.response.*;
import cn.psvmc.cxzapi.service.customer.TCustomerBuyoutService;
import cn.psvmc.cxzapi.util.PageUtil;
import cn.psvmc.cxzapi.util.ToolUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * 买断申请管理
 * @Author auto
 * @Date 2024-08-12
 */
@RestController
@RequestMapping("/buyout")
public class TCustomerBuyoutController {

    @Resource
    private TCustomerBuyoutService tCustomerBuyoutService;

    /**
    * @Author auto 2024-08-12 新增客户买断申请表
    * @Param tCustomBuyout 客户买断申请表
    * @Return cn.psvmc.cxzapi.response.Result
    */
    @PostMapping("/add")
    public Result add(@RequestBody TCustomerBuyout tCustomerBuyout) {
        int addResult = tCustomerBuyoutService.add(tCustomerBuyout);
        if (addResult > 0) {
            return ResultGenerator.genSuccessResult("保存成功！");
        }
        return ResultGenerator.genFailResult("保存失败！");
    }

    /**
    * @Author auto 2024-08-12 更新客户买断申请表
    * @Param tCustomBuyout 客户买断申请表模型
    * @Return cn.psvmc.cxzapi.response.Result
    */
    @PostMapping("/edit")
    public Result edit(@RequestBody TCustomerBuyout tCustomerBuyout) {
        ToolUtils.validation(new Object[]{tCustomerBuyout.getCbid(), "id", 1, tCustomerBuyout.getRversion(), "版本", 1});
        int updateResult = tCustomerBuyoutService.edit(tCustomerBuyout);
        if (updateResult > 0) {
            return ResultGenerator.genSuccessResult("更新成功！");
        }
        return ResultGenerator.genFailResult("更新失败！");
    }

    /**
    * @Author auto 2024-08-12 查询客户买断申请表详情
    * @Param [entity]
    * @Return cn.psvmc.cxzapi.response.Result
    */
    @PostMapping("/detail")
    public Result detail(@RequestBody DetailEntity entity) {
        Integer id = entity.getId();
        ToolUtils.validation(new Object[]{id, "id", 1});
        return ResultGenerator.genSuccessResult("查询成功", tCustomerBuyoutService.detail(id));
    }

    /**
    * @Author auto 2024-08-12 查询客户买断申请表列表
    * @Param entity 分页查询通用实体类
    * @Return cn.psvmc.cxzapi.response.Result
    */
    @PostMapping("/list")
    public PageResult2 list(@RequestBody PageEntity entity) {
        Page<TCustomerBuyout> queryPage = new Page<>(entity.getPage(), entity.getSize());
        return PageUtil.getPageResult2(tCustomerBuyoutService.list(queryPage));
    }
}
